package com.generator.storageManage.outboundManage.service.impl;


import com.generator.storageManage.outboundManage.dao.IOutBoundManage;
import com.generator.storageManage.outboundManage.pojo.CommentDetial;
import com.generator.storageManage.outboundManage.pojo.OutBoundObject;
import com.generator.storageManage.outboundManage.pojo.OutCountDelivery;
import com.generator.storageManage.outboundManage.service.IOutBoundManageService;
import com.generator.util.PageModel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class OutBoundManageService implements IOutBoundManageService {

    @Autowired
    private IOutBoundManage outBoundManage;

    public IOutBoundManage getOutBoundManage() {
        return outBoundManage;
    }

    public void setOutBoundManage(IOutBoundManage outBoundManage) {
        this.outBoundManage = outBoundManage;
    }

    //根据条件查询出库管理
    @Override
    public List<OutBoundObject> queryOutBoundList(OutBoundObject outBoundObject) {

        outBoundObject.setDdstate(0);
        List<OutBoundObject> outBoundObjects = outBoundManage.queryOutBoundList(outBoundObject);
        for(OutBoundObject boundObject : outBoundObjects) {
            switch (boundObject.getStateid()){
                case 1:boundObject.setState("未发货");break;
                case 2:boundObject.setState("已发货");break;
                case 3:boundObject.setState("已回款");break;
                case 4:boundObject.setState("取消订单");break;
                case 5:boundObject.setState("已退货");break;
                default:boundObject.setState("错！！！");
            }
        }

        return outBoundObjects;
    }

    //更新出库状态
    @Override
    public Integer updateOutBoundState(Integer OutBoundId,Integer stateid) {
        OutBoundObject outBoundObject = new OutBoundObject();
        outBoundObject.setOutboundid(OutBoundId);
        List<OutBoundObject> outBoundObjects = queryOutBoundList(outBoundObject);
        outBoundObjects.get(0).setStateid(stateid);
        return outBoundManage.updateOutBoundState(outBoundObjects.get(0));
    }

    //取得订单的所有信息
    @Override
    public OutCountDelivery getDeliveryByOrderId(OutCountDelivery outCountDelivery) {
        List<OutCountDelivery> deliveryByOrderId = outBoundManage.getDeliveryByOrderId(outCountDelivery);


        float totalprice=0;
        for (OutCountDelivery countDelivery : deliveryByOrderId) {
            totalprice+=countDelivery.getTotalprice();
        }
        OutCountDelivery outCountDelivery1=null;
        if(deliveryByOrderId.size()>0){
            outCountDelivery1 = deliveryByOrderId.get(0);
            outCountDelivery1.setTotalprice(totalprice);
            Integer outCountZhuangTaiId = outCountDelivery1.getOutCountZhuangTaiId();
            Integer shengHeZhuangTaiId = outCountDelivery1.getShengHeZhuangTaiId();
            if(outCountZhuangTaiId==null){
                outCountZhuangTaiId=1;
            }
            switch (outCountZhuangTaiId){
                case 1 :outCountDelivery1.setOutCountZhuangTai("未发货");break;
                case 2 :outCountDelivery1.setOutCountZhuangTai("已发货");break;
                case 3 :outCountDelivery1.setOutCountZhuangTai("已回款");break;
                case 4 :outCountDelivery1.setOutCountZhuangTai("取消订单");break;
                case 5 :outCountDelivery1.setOutCountZhuangTai("已退货");break;
                default:
                    System.out.println("你什么问题？");
            }

            switch (shengHeZhuangTaiId){
                case 0:outCountDelivery1.setShengHeZhuangTai("未审核");break;
                case 1:outCountDelivery1.setShengHeZhuangTai("审核中");break;
                case 2:outCountDelivery1.setShengHeZhuangTai("审核通过");break;
                case 3:outCountDelivery1.setShengHeZhuangTai("审核不通过");break;
                default:
                    System.out.println("你什么大病？");
            }
        }
        return outCountDelivery1;
    }

    //取得订单对应的商品信息
    @Override
    public List<CommentDetial> getCommentDetialByOrderId(CommentDetial commentDetial) {
        return outBoundManage.getCommentDetialByOrderId(commentDetial);
    }

    //取得所有仓库名称
    @Override
    public List<String> getStorageName() {
        return outBoundManage.getStorageName();
    }

    //根据仓库名查询仓库id
    @Override
    public Integer queryStorageIdByStorageName(String storagename) {
        return outBoundManage.queryStorageIdByStorageName(storagename);
    }

    //根据用户名查询用户id
    @Override
    public Integer queryUserIdByUserName(String username) {
        return outBoundManage.queryUserIdByUserName(username);
    }

    //添加出库
    @Override
    public Integer addOutBound(OutBoundObject outBoundObject) {
        Integer storageid = queryStorageIdByStorageName(outBoundObject.getStoragename());
        Integer outboundmanid = queryUserIdByUserName(outBoundObject.getOutboundman());
        outBoundObject.setStorageid(storageid);
        outBoundObject.setOutboundmanid(outboundmanid);
        updateOutBoundlevel(outBoundObject.getOrderid());
        return outBoundManage.addOutBound(outBoundObject);
    }

    //改变订单出库状态
    @Override
    public Integer updateOutBoundlevel(String orderid) {
        return outBoundManage.updateOutBoundlevel(orderid);
    }


    //分页
    public PageModel<OutBoundObject> getOutBoundpage(PageModel<OutBoundObject> pageModel,OutBoundObject outBoundObject){

        pageModel.setPageNum(pageModel.getPageNum()<=0?1:pageModel.getPageNum());
        pageModel.setPageSize(pageModel.getPageSize()<=0?3:pageModel.getPageSize());
        PageHelper.startPage(pageModel.getPageNum(), pageModel.getPageSize());
        List<OutBoundObject> outBoundObjects = queryOutBoundList(outBoundObject);
        PageInfo<OutBoundObject> warehousePageInfo = new PageInfo<OutBoundObject>(outBoundObjects);

        pageModel.setData(new ArrayList<OutBoundObject>(outBoundObjects));
        pageModel.setTotalPage(warehousePageInfo.getPages());
        pageModel.setTotalRows(warehousePageInfo.getTotal());
        return pageModel;
    }
}
